import Vue from 'vue';
export default {
    namespaced: true,
    state() {
        return {
            'cartList': [
                {
                    'gid': 120054,
                    'img': "https://img2.epetbar.com/common/upload/commonfile/2020/02/26/162433_849923.jpg",
                    'title': "加拿大原装进口 爱肯拿Acana 无谷鸭肉巴特利梨配方全犬粮 2kg",
                    'subtitle': "加拿大渴望狗粮同厂出品原装进口",
                    'price': 99.00,
                    'count': 1,
                    'format': '2kg',
                    'isCheck': true,
                }
            ]
        }
    },
    getters: {
        info(state) {
            let { cartList } = state;
            let cartCount = 0;
            let totalMoney = 0;
            let allCheck = true;
            let checkItem=[]
            cartList.forEach(item => {
                if (!item.isCheck) {
                    allCheck = false
                }
                if (item.isCheck) {
                    cartCount += item.count;
                    totalMoney += item.price * item.count;
                    checkItem.push(item)
                }
            })
            return {
                cartCount,
                totalMoney,
                allCheck,
                checkItem
            }
        }
    },
    mutations: {
        goodAddMut(state, obj) {
            let { gitem, value, forMat } = obj
            let { cartList } = state;
            let item = {};
            Vue.set(item, 'gid', gitem[1].gid)
            Vue.set(item, 'img', gitem[0].photos[0].image)
            Vue.set(item, 'title', gitem[1].subject.replace('2kg', forMat))
            Vue.set(item, 'subtitle', gitem[1].presubject)
            Vue.set(item, 'price', 99)
            Vue.set(item, 'count', value)
            Vue.set(item, 'format', forMat)
            Vue.set(item, 'isCheck', true)
            cartList.push(item)
        },
        goodIncrease(state, idx) { //商品数量加
            state.cartList[idx].count++
        },
        goodDecrease(state, idx) {//商品数量减
            if (state.cartList[idx].count > 1) {
                state.cartList[idx].count--
            }
        },
        singleCheck(state, idx) {//单选
            state.cartList[idx].isCheck = !state.cartList[idx].isCheck
        },
        checkAll(state, bool) {
            state.cartList.forEach(item => {
                item.isCheck = !bool
            })
        }
    }
}